/**
 * Embeded script file that is embeded from the content script 
 * into the web page to test the global window object for 
 * javascript libraries.
 * 
 * There�s one catch in some libraries: 
 * If the developer renamed the scope of the Library (i.e. window.dojo) 
 * to something entirely different, the test fails.
 * 
 * Currently the script tests for the following libraries:
 * 	-jQuery
 * 	-jQuery UI
 * 	-dojo
 * 	-ExJs
 * 	-YUI
 * 	-GWT
 * 	-Prototype
 * 	-Scriptaculous
 * 	-MooTools
 * 	-Modernizr
 * 
 * If one of these is found on the current page, it stores the library and 
 * it's version into an object, that is later stored into the localStorage 
 * of the page. If no library is detected an empty string is stored into the 
 * localStorage.
 * 
 */

;(function(){
	var localStorageKey = "LD",
		oldStorageValue = localStorage[localStorageKey],
		libs = {
			"jQuery" : {
				icon : "jquery",
				test : function(win) {
					var jq = win.jQuery || win.$ || win.$jq || win.$j;
					if(jq && jq.fn && jq.fn.jquery) {
						return { version: jq.fn.jquery };
					} else {
						return false;
					}
				}
			},
			
			"jQuery UI" : {
				icon : "jquery-ui",
				test: function(win) {
					
					var jq = win.jQuery || win.$ || win.$jq || win.$j;
					if(jq && jq.fn && jq.fn.jquery && jq.ui) {

						var plugins = 'accordion,datepicker,dialog,draggable,droppable,progressbar,resizable,selectable,slider,menu,grid,tabs'.split(','), concat = [];
						for (var i=0; i < plugins.length; i++) { if(jq.ui[plugins[i]]) concat.push(plugins[i].substr(0,1).toUpperCase() + plugins[i].substr(1)); };
					
						return { version: jq.ui.version, details: concat.length ? 'Plugins used: '+concat.join(',') : '' };
					} else {
						return false;
					}
					
				}
			},
			
			"Dojo" : {
				icon : "dojo",
				test : function(win) {
					if(win.dojo || win.djConfig) {
						return win.dojo ? { 
							version: win.dojo.version.toString(), 
							details: 'Details: '+(win.dijit ? 'Uses Dijit' : 'none')
						} : {
							version: "(not detectable)"
						};
					} else {
						return false;
					}
				}
			},
			
			"Prototype" : {
				icon : "prototype",
				test : function(win) {
					if(win.Prototype && win.Prototype.Version) {
						return { version: win.Prototype.Version };
					} else {
						return false;
					}
				}
			},
			
			"Scriptaculous" : {
				icon : "scriptaculous",
				test : function(win) {
					if(win.Scriptaculous && win.Scriptaculous.Version) {
						return { version: win.Scriptaculous.Version };
					} else {
						return false;
					}
				}
			},
			
			"MooTools" : {
				icon : "mootools",
				test : function(win) {
					if(win.MooTools && win.MooTools.version) {
						return { version: win.MooTools.version };
					} else {
						return false;
					}
				}
			},
			
			"YUI" : {
				icon : "yui",
				test : function(win) {
					if(win.YAHOO && win.YAHOO.VERSION) {
						return { version: win.YAHOO.VERSION };
					} else {
						return false;
					}
				}
			},
			
			"Ext JS" : {
				icon : "extjs",
				test : function(win) {
					if(win.Ext && win.Ext.version) {
						return { version: win.Ext.version };
					} else {
						return false;
					}
				}
			},
			
			"Modernizr" : {
				icon : "modernizr",
				test : function(win) {
					if(win.Modernizr) {
						return { version: win.Modernizr._version };
					}
					return false;
				}
			},

			"GWT" : {
				icon : "gwt",
				test : function(win) {
					return win.__gwt_scriptsLoaded ? {version: '(not detectable)' } : false;
				}
			}	
	},
	libs_passed = {};
	
	/**
	 * 
	 */
	function triggerStorageEvent() {
		var storageEvent = document.createEvent("StorageEvent");
		storageEvent.initStorageEvent("storage", false, false, "LD", oldStorageValue, localStorage[localStorageKey], null, null);
		document.body.dispatchEvent(storageEvent);
	}
	
	/**
	 * 
	 */
	function perform_tests() {
		//ensure that the dom is completely loaded
		if (window.performance.timing.loadEventEnd === 0) {
			window.setTimeout(perform_tests, 50);
			return;
		} else {
			var lib, version, i = 0, stringify = "";
			
			for (lib in libs) {
				if (libs[lib].test && (version = libs[lib].test(window))) {
					libs_passed[lib] = {
							version : version.version,
							icon : libs[lib].icon
					};
				};
			};
			
			stringify = JSON.stringify(libs_passed);
			
			//set localStorage
			localStorage.setItem(localStorageKey, stringify === "{}" ? "" : stringify);
			/**
			 * Trigger manually onstorage event as currently it is not 
			 * triggered in Chrome, it is not supported.
			 */
			if (localStorage["onstorage"] === undefined) {
				triggerStorageEvent();
			}
		}
	}
	
	/**
	 * 
	 */
	function init() {
		//start testing
		perform_tests();
	}
	
	init();
})();
